package com.mlethe.library.fingerprint.uitls;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;

import androidx.core.hardware.fingerprint.FingerprintManagerCompat;

/**
 * 描述手机信息的检测对象
 * @author Mlethe
 */
public class FingerprintUtil {

    private static final String SONY = "sony";
    private static final String OPPO = "oppo";
    private static final String HUAWEI = "huawei";
    private static final String HONOR = "honor";
    private static final String ONEPLUS = "oneplus";
    private static final String VIVO = "vivo";

    /**
     * 跳转到指纹页面 或 通知用户去指纹录入
     */
    public static void startFingerprint(final Context context) {
        String pcgName = null;
        String clsName = null;
        if (compareTextSame(SONY)){
            pcgName = "com.android.settings";
            clsName = "com.android.settings.Settings$FingerprintEnrollSuggestionActivity";
        } else if (compareTextSame(OPPO)) {
            pcgName = "com.coloros.fingerprint";
            clsName = "com.coloros.fingerprint.FingerLockActivity";
        } else if (compareTextSame(HUAWEI)) {
            pcgName = "com.android.settings";
            clsName = "com.android.settings.fingerprint.FingerprintSettingsActivity";
        } else if (compareTextSame(HONOR)) {
            pcgName = "com.android.settings";
            clsName = "com.android.settings.fingerprint.FingerprintSettingsActivity";
        }
        // TODO 后续机型会继续加入的 （Deliliu）
        // ....
        else {
            // 如果以上判断没有符合该机型，那就跳转到设置界面，让用户自己设置吧
            // Toast.makeText(context, "请到设置中，找到指纹录入，进行指纹录入操作", Toast.LENGTH_LONG).show();
            new AlertDialog.Builder(context)
                    .setTitle("指纹录入")
                    .setMessage("请到设置中，找到指纹录入，进行指纹录入操作")
                    .setPositiveButton("好的，我去录入指纹", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String pcgName = "com.android.settings";
                            String clsName = "com.android.settings.Settings";
                            startActivity(context, pcgName, clsName);
                        }
                    })
                    .show();

            // 跳转到Settings页面的Intent
            /*pcgName = "com.android.settings";
            clsName = "com.android.settings.Settings";*/
        }
        startActivity(context, pcgName, clsName);
    }

    private static void startActivity(Context context, String pcgName, String clsName) {
        if (!TextUtils.isEmpty(pcgName) && !TextUtils.isEmpty(clsName)) {
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName(pcgName, clsName);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setComponent(componentName);
            context.startActivity(intent);
        }
    }

    /**
     * 对比两个字符串，并且比较字符串是否包含在其中的，并且忽略大小写
     * @param value
     * @return
     */
    private static boolean compareTextSame(String value) {
        return value.toUpperCase().indexOf(Build.BRAND.toUpperCase()) >= 0 ;
    }

    /**
     * 检测指纹硬件是否可用
     * @param context
     * @return
     */
    public static boolean canAuthenticate(Context context) {
        /**
         * 硬件是否支持指纹识别
         */
        if (!FingerprintManagerCompat.from(context).isHardwareDetected()) {
            return false;
        }
        return true;
    }
}